<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * The NoteNews class for table 'notenews'.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.2
 * 
 * 0.2) Inheriting from BaseNews instead of Original DataObject.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_basenews.php';

// Definitions.
// Column of status reserves.

class NoteNews extends BaseNews
{
	public $__table = 'notenews';			// table name
	public $id;								// int(11)  primary_key not_null
	public $news_id;						// int(11)  foreign_key unique not_null
	public $user_id;						// int(11)  foreign_key not_null
	public $message;						// text
	public $rendered;						// text
	public $viewcnt;						// int(11)
	public $created;						// datetime()  not_null
	
	// Static get
	function staticGet($key, $value = null)
	{
		return Memcached_DataObject::staticGet('NoteNews', $key, $value); 
	}
	
	/**
	 * Increase the view count of the note news by 1.
	 */
	function increaseViewCount()
	{
		$query = 'UPDATE `notenews` SET `viewcnt` = `viewcnt`+1 ' .
				 'WHERE `news_id` = ' . $this->news_id;
		
		$notenews = new NoteNews();
		return $notenews->query($query);
	}
	
	/**
	 * Add the notes news into database.
	 * 
	 * $fields includes: $message, $rendered, $user_id;
	 */
	static function add($fields)
	{
		// Magically put fields into current scope.
		extract($fields);
		
		$news = new News();
		$news->newstype = NEWS_NEWSTYPE_NOTENEWS;
// TODO: The other variables.
		$news->newstag_id = 0;
		$news->status = 0;
		
		$news_id = $news->insert();
		if (empty($news_id)) {
			slh_log_db_error($news, 'INSERT', __FILE__);
			return false;
		}
		$news->id = $news_id;
		
		$notenews = new NoteNews();
		$notenews->news_id = $news_id;
		$notenews->user_id = $user_id;
		$notenews->message = $message;	
		$notenews->rendered = $rendered;	
		$notenews->created = slh_sql_now();
		
		$notenews_id = $notenews->insert();
		if (empty($notenews_id)) {
			slh_log_db_error($notenews, 'INSERT', __FILE__);
			
			@$news->delete();
			return false;
		}
		
		return $notenews_id;
	}
	
	/**
	 * Get the ids as a list. Use offset and limit to control
	 * the pagination.
	 * 
	 * Returning the 'news_id', not the original 'id'.
	 */
	static function getNewsidsByList($offset = 0,
									 $limit = 0)
	{
		return parent::getNewsidsByList(new self(), $offset, $limit);
	}
	
	/**
	 * Get the event news objects by the given newsids.
	 */
	static function getNotenewsByNewsids($newsids)
	{
		return parent::getSubnewsByNewsids('NoteNews', $newsids);
	}
}
?>